package com.ruoyi.contarctbill.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.enums.SystemStatusType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtils;
import com.ruoyi.contarct.domain.HouseContarct;
import com.ruoyi.contarct.enums.IncomeFeeType;
import com.ruoyi.contarct.enums.IncomeFlowType;
import com.ruoyi.contarct.mapper.HouseContarctMapper;
import com.ruoyi.contarctbill.dto.CustomBillDTO;
import com.ruoyi.contarctbill.dto.CustomInComeDTO;
import com.ruoyi.contarctbill.enums.BillIsoverType;
import com.ruoyi.contarctbill.enums.BillRefundStatusType;
import com.ruoyi.contarctbill.enums.BillStatusType;
import com.ruoyi.contarctbill.enums.BillType;
import com.ruoyi.income.domain.HouseContarctIncome;
import com.ruoyi.income.mapper.HouseContarctIncomeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.contarctbill.mapper.HouseContarctBillMapper;
import com.ruoyi.contarctbill.domain.HouseContarctBill;
import com.ruoyi.contarctbill.service.IHouseContarctBillService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账单Service业务层处理
 * 
 * @author hct
 * @date 2022-02-16
 */
@Service
public class HouseContarctBillServiceImpl implements IHouseContarctBillService 
{
    @Autowired
    private HouseContarctBillMapper houseContarctBillMapper;

    @Autowired
    private HouseContarctIncomeMapper houseContarctIncomeMapper;

    @Autowired
    private HouseContarctMapper houseContarctMapper;

    /**
     * 查询账单
     * 
     * @param id 账单主键
     * @return 账单
     */
    @Override
    public HouseContarctBill selectHouseContarctBillById(Long id)
    {
        return houseContarctBillMapper.selectHouseContarctBillById(id);
    }

    /**
     * 查询账单列表
     * 
     * @param houseContarctBill 账单
     * @return 账单
     */
    @Override
    public List<HouseContarctBill> selectHouseContarctBillList(HouseContarctBill houseContarctBill)
    {
        List<HouseContarctBill> houseContarctBills = houseContarctBillMapper.selectHouseContarctBillList(houseContarctBill);
        return houseContarctBills;
    }


    @Override
    public List<HouseContarctBill> selectHouseContarctBillAllData(List<HouseContarct> houseContarcts, HouseContarctBill houseContarctBill) {
        Map<Long, HouseContarct> contarctMap = houseContarcts.stream().collect(Collectors.toMap(HouseContarct::getId, Function.identity(), (key1, key2) -> key2));
        List<Long> contarctIds = houseContarcts.stream().map(HouseContarct::getId).collect(Collectors.toList());
        Long [] conIds =new Long [contarctIds.size()];
        contarctIds.toArray(conIds);
        //根据月份获取账单时间区间
        Date start = null;
        Date end = null;
        String billMonth = houseContarctBill.getBillMonth();
        if(StringUtils.isNotEmpty(billMonth)){
            Date billMonthDate = DateUtils.dateTime("yyyy-MM", billMonth);
            try {
                start = DateUtils.currMonthFirst(billMonthDate,"yyyy-MM-dd hh:mm:ss");
                end = DateUtils.currMonthLast(billMonthDate,"yyyy-MM-dd hh:mm:ss");
            } catch (ParseException e) {
            }
        }
        List<HouseContarctBill> houseContarctBills = houseContarctBillMapper.selectBillById(conIds,start,end);
        for (HouseContarctBill bill : houseContarctBills
             ){
            BigDecimal billShould = new BigDecimal(0D);//应收
            BigDecimal billAlready = new BigDecimal(0D);//已收
            List<HouseContarctIncome> houseContarctIncomes = bill.getHouseContarctIncomes();
            if(houseContarctIncomes != null && houseContarctIncomes.size() > 0){
                for (HouseContarctIncome icome :houseContarctIncomes
                ){
                    if(icome != null && icome.getFee() != null && icome.getYsfee() != null){
                        billShould = billShould.add(icome.getFee());
                        billAlready = billAlready.add(icome.getYsfee());
                    }
                }
            }
            bill.setBillShould(billShould);
            bill.setBillAlready(billAlready);
            HouseContarct contarct = contarctMap.get(bill.getContarctId());
            bill.setHouseContarct(contarct);
        }
        return houseContarctBills;
    }

    /**
     * 新增账单
     * 
     * @param houseContarctBill 账单
     * @return 结果
     */
    @Override
    public int insertHouseContarctBill(HouseContarctBill houseContarctBill)
    {
        return houseContarctBillMapper.insertHouseContarctBill(houseContarctBill);
    }

    /**
     * 修改账单
     * 
     * @param houseContarctBill 账单
     * @return 结果
     */
    @Override
    public int updateHouseContarctBill(HouseContarctBill houseContarctBill)
    {
        return houseContarctBillMapper.updateHouseContarctBill(houseContarctBill);
    }

    /**
     * 批量删除账单
     * 
     * @param ids 需要删除的账单主键
     * @return 结果
     */
    @Override
    public int deleteHouseContarctBillByIds(Long[] ids)
    {
        return houseContarctBillMapper.deleteHouseContarctBillByIds(ids);
    }

    /**
     * 删除账单信息
     * 
     * @param id 账单主键
     * @return 结果
     */
    @Override
    public int deleteHouseContarctBillById(Long id)
    {
        return houseContarctBillMapper.deleteHouseContarctBillById(id);
    }


    @Override
    public Object getCurrMonthBiiList() throws Exception {
        Date now = new Date();
        HouseContarctBill currMonthContarctBill = houseContarctBillMapper.getCurrMonthContarctBill(now);
        return currMonthContarctBill;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addCustomBill(CustomBillDTO customBillDTO, String userName) throws Exception {
        Date now = new Date();
        Double feed = 0D;
        List<CustomInComeDTO> incomeList = customBillDTO.getIncomeList();
        for (CustomInComeDTO customInComeDTO: incomeList
             ){
            feed = MoneyUtils.add(feed,customInComeDTO.getFee());
        }
        feed = MoneyUtils.div(feed,1D,2);
        HouseContarctBill bill = new HouseContarctBill();
        bill.setFee(new BigDecimal(feed));
        bill.setContarctId(customBillDTO.getConctartId());
        bill.setContarctPlanbz(customBillDTO.getBillName());
        bill.setContarctPlandate(customBillDTO.getBillName());
        bill.setIsover(BillIsoverType.NOTOVER.getCode());
        bill.setYsfee(new BigDecimal(0D));
        bill.setContarctPlanEnddate(customBillDTO.getContarctPlanEnddate());
        bill.setContarctPlanStartdate(customBillDTO.getContarctPlanStartdate());
        bill.setRefundStatus(Long.parseLong(BillRefundStatusType.NORETFUNDSETTLE.getCode()+""));
        bill.setStatus(Long.parseLong(BillStatusType.BILLAVAILABLE.getCode()+""));
        bill.setBillName(userName);
        bill.setBillType(BillType.CUSTOM.getCode());
        bill.setCreateBy(userName);
        bill.setUpdateBy(userName);
        bill.setCreateTime(now);
        bill.setUpdateTime(now);
        String start = DateUtils.dateTime(customBillDTO.getContarctPlanStartdate());
        String end = DateUtils.dateTime(customBillDTO.getContarctPlanEnddate());
        bill.setContarctPlanbz(start+"~"+end);
        bill.setContarctPlandate(start+"~"+end);
        bill.setBillName("自定义账单");
        bill.setBillType(BillType.CUSTOM.getCode());
        bill.setEndTime(customBillDTO.getContarctPlanEnddate());
        try {
            houseContarctBillMapper.insertHouseContarctBill(bill);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (CustomInComeDTO customInComeDTO: incomeList
        ){
            HouseContarctIncome income = new HouseContarctIncome();
            income.setFee(new BigDecimal(customInComeDTO.getFee()));
            income.setContarctId(customBillDTO.getConctartId());
            income.setBillId(bill.getId());
            income.setYsfee(new BigDecimal(0D));
            income.setFlowtype(Long.parseLong(IncomeFlowType.IN.getCode()+""));
            income.setFeetype(IncomeFeeType.CHANGE.getCode());
            income.setFeetypeid(customInComeDTO.getFeetypeid());
            income.setCreateBy(userName);
            income.setUpdateBy(userName);
            income.setCreateTime(now);
            income.setUpdateTime(now);
            income.setContarctPlandate(income.getContarctPlandate());
            income.setStatus(SystemStatusType.ENABLE.getCode());
            income.setContarctPlandate(start+"~"+end);
            houseContarctIncomeMapper.insertHouseContarctIncome(income);
        }
        return bill;
    }
    public List<HouseContarctBill> selectHouseContarctBillListBy(HouseContarctBill houseContarctBill){
        return houseContarctBillMapper.selectHouseContarctBillListBy(houseContarctBill);
    }
}
